package com.goldmantis.alitrip.service;

import com.alibaba.fastjson.JSON;
import com.goldmantis.alitrip.common.BaseService;
import com.goldmantis.alitrip.pojo.AlitripBtripApprovalModel;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.internal.util.StringUtils;
import com.taobao.api.request.AlitripBtripApprovalNewRequest;
import com.taobao.api.request.AlitripBtripApprovalUpdateRequest;
import com.taobao.api.response.AlitripBtripApprovalNewResponse;
import com.taobao.api.response.AlitripBtripApprovalUpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 17:56 2018/1/15
 */
@Service
public class ApprovalNewService extends BaseService {

    @Autowired
    private  TaobaoClient client;

    @Autowired
    private ApprovalUpdateService approvalUpdateService;

    private static final Logger logger = LoggerFactory.getLogger(ApprovalNewService.class);

    public AlitripBtripApprovalModel insertNewApproval(
            AlitripBtripApprovalNewRequest.OpenAddApplyRq applyRequest,
            List<AlitripBtripApprovalNewRequest.OpenItineraryInfo> itineraryInfoList,
            List<AlitripBtripApprovalNewRequest.OpenUserInfo> travelerList)  throws Exception {

        applyRequest.setCorpId(corp_id);
        applyRequest.setCorpName(corp_name);

        AlitripBtripApprovalModel alitripBtripApprovalModel=new AlitripBtripApprovalModel();
        AlitripBtripApprovalNewRequest req = new AlitripBtripApprovalNewRequest();
        applyRequest.setItineraryList(itineraryInfoList);
        applyRequest.setTravelerList(travelerList);

        req.setAddApplyRequest(applyRequest);
        AlitripBtripApprovalNewResponse rsp = client.execute(req, accessToken);
        alitripBtripApprovalModel.setAlitripBtripApprovalNewResponse(rsp);

        logger.info("审批单："+applyRequest.getApplyId()+"提交成功。"+ JSON.toJSONString(req));

        if(rsp!=null&&rsp.getResult().getSuccess()) {
            //提交成功后，进入审批操作。
            AlitripBtripApprovalUpdateRequest.OpenApproveApplyRq approveApplyRequest = new AlitripBtripApprovalUpdateRequest.OpenApproveApplyRq();
            approveApplyRequest.setApplyId(applyRequest.getApplyId());
            approveApplyRequest.setStatus(1L);  //1已同意  默认同意。
            approveApplyRequest.setNote("系统默认同意");
            approveApplyRequest.setApprovalUserName("系统管理员");
            approveApplyRequest.setApprovalUserId("0442165202482222255");
            AlitripBtripApprovalUpdateResponse alitripBtripApprovalUpdateResponse = approvalUpdateService.updateApproval(approveApplyRequest);
            alitripBtripApprovalModel.setAlitripBtripApprovalUpdateResponse(alitripBtripApprovalUpdateResponse);

            logger.info("审批单："+applyRequest.getApplyId()+"审批成功。"+JSON.toJSONString(req));
        }
        return alitripBtripApprovalModel;
    }
}
